#pragma once

#include <iostream>
#include "log.hpp"
#include <string>
#include "Procotol.hpp"

// enum
// {
//     Div_Zero = 1,
//     Mod_Zero,
//     Res_Reliable
// };

class Servercal
{
public:
    Servercal()
    {
    }
    ~Servercal()
    {
    }

public:
    std::string Calculator(std::string &package)
    {
        std::string content;
        //std::cout<<content<<std::endl;
        if (!Decode(package, &content))
        {
            //std::cerr << "Decode failed\n";
            return "";
        }

        Request Req(0,'+',0);
        if(!Req.Deserialization(content))
        {
            //std::cerr << "Deserialization failed\n";
            return "";
        }
        content="";
        Response Res = CalculatorHelper(Req);
        //std::cout<<Res._result<<":"<<Res._code<<std::endl;
        //std::string tmp;
        Res.Serialize(&content);
        content=Encode(content);
        //std::cout<<content<<std::endl;
        return content;
    }

private:
    Response CalculatorHelper(const Request &Req)
    {
        Response Res("Res_Reliable",0);
        switch (Req._op)
        {
        case '+':
            Res._code = Req._x + Req._y;
            break;
        case '-':
            Res._code = Req._x - Req._y;
            break;
        case '*':
            Res._code = Req._x * Req._y;
            break;
        case '%':
        {
            if (Req._y == 0)
                Res._result = "Mod_Zero";
            else
                Res._code = Req._x % Req._y;
        }
        break;
        case '/':
        {
            if (Req._y == 0)
                Res._result = "Div_Zero";
            else
                Res._code = Req._x / Req._y;
        }
        break;
        default:
            break;
        }

        return Res;
    }
};